package storeSorting

import (
	"context"
	"github.com/google/uuid"
	"github.com/kuangshp/go-utils/k"
	"insighttracking/common/utils"
	"insighttracking/query/model"
	"insighttracking/query/service"
	"net/http"

	"insighttracking/apps/sorting/internal/svc"
	"insighttracking/apps/sorting/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetStoreSortingListApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetStoreSortingListApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetStoreSortingListApiLogic {
	hostname, _ := os.Hostname()
	return &GetStoreSortingListApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetStoreSortingListApiLogic) GetStoreSortingListApi(req *types.GetStoreSortingListReq) (resp []types.GetStoreSortingListResp, err error) {
	ordersDetailList, err := service.StoreSortingDetailService.GetStoreSortingDetailBySpuIDApi(l.ctx, req.OrdersID, req.SpuID)
	if err != nil || len(ordersDetailList) == 0 {
		return make([]types.GetStoreSortingListResp, 0), nil
	}
	return k.Map(ordersDetailList, func(item *model.StoreSortingDetailEntity, index int) types.GetStoreSortingListResp {
		return types.GetStoreSortingListResp{
			ID:             item.ID,                                 // 主键id
			OrdersDetailID: item.OrdersDetailID,                     // 关联到orders_detail表主键id
			SpuID:          item.SpuID,                              // 商品,关联到spu表主键id
			OutboundCount:  item.OutboundCount.StringFixed(2),       // 出库数量
			UUID:           item.UUID,                               // 唯一编码
			CreatedAt:      utils.TimestampToString(item.CreatedAt), // 创建时间
			IndexNo:        int64(index + 1),
		}
	}), nil
}
